package com.pig4cloud.pig.admin.task;

import com.pig4cloud.pig.admin.api.entity.NewsList;
import com.pig4cloud.pig.admin.constant.CommonConstant;
import com.pig4cloud.pig.admin.task.base.BaseTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class BaiDuNews extends BaseTask {
	private final String url = "https://top.baidu.com/board?tab=realtime";

	@Scheduled(cron = "0 0/5 * * * ?")
	public void doTask() {
		String html = httpUtilService.getRequest(1,BaiDuNews.class,url, CommonConstant.UTF8);
		Document parse = Jsoup.parse(html);
		Element element = parse.getElementById("sanRoot");
		Elements elementsByClass = element.getElementsByClass("horizontal_1eKyQ");
		for (Element element2 : elementsByClass) {
			NewsList news = NewsList.create(idGenerator.nextId());
			Element element3 = element2.getElementsByTag("a").get(0);
			String title = element2.getElementsByClass("c-single-text-ellipsis").text();
			String imgUrl = element2.select("img:nth-child(2)").attr("src");
			String desc = element2.getElementsByClass("ellipsis_DupbZ").text();
			String href = element3.attr("href").toString();
			news.setTitle(title);
			news.setSubTitle(desc);
			news.setCreateTime(CommonConstant.getDay());
			news.setUrl(href);
			news.setImgUrl(imgUrl);
			news.setCategory("百度热搜");
			pushNewsToRedis(news);
		}
    }

}
